<?php
namespace app\backstage\logic;
use think\Db;
class BookSummary
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'report_date,new_user_num,nid,accumulated_recharge_money,cost,profit,t2,t3,t4,t5,t6';

        $groupBy = '';
        $type = 3;
        $uid = request()->_user->id;

        if(isset($params['channel_id']) && $params['channel_id'] > 0)
        {
            $type = 2;
        }else{
            if(request()->_role->alias == 'agent')
            {
                $type = 1;
                $params['agent_id'] = $uid;
            }
            if(request()->_role->alias == 'channel')
            {
                $params['channel_id'] = $uid;
                $type = 2;
            }
        }

        $condition[] = ['agent_id', 'eq', 'gt:0', 'agent_id'];
        $condition[] = ['channel_id', 'eq', 'gt:0', 'channel_id'];
        $condition[] = ['nid', 'eq', 'gt:0','nid'];

        $where   = buildQueryCondition($condition, $params);

        if (isset($params['field'])) {
            $orderBy = [$params['field'] => $params['order']];
        } else {
            $orderBy = ['report_date' => 'DESC'];
        }

        $BookSummary = \think\facade\App::model('BookSummary');
        $list = $BookSummary->getList($type, $where, $field, $params['page'], $params['limit'], $orderBy, $groupBy);

        if ($list) {

            $listAll = $BookSummary->getAll($type,$where, $field, $orderBy, $groupBy);

            $nid_gh = array_unique(array_column($list,'nid'));
            $novels = Db::table('novel')->where('id','in',$nid_gh)->column('id,title,freq_id,class_id,son_class_id');

            $cat_sonid_gh = array_unique(array_column($novels,'son_class_id'));
            $cats_son = Db::table('novel_class_son')->where('id','in',$cat_sonid_gh)->column('id,name');

            $cat_id_gh = array_unique(array_column($novels,'class_id'));
            $cats = Db::table('novel_class')->where('id','in',$cat_id_gh)->column('id,name');

            $freq_id_gh = array_unique(array_column($novels,'freq_id'));
            $freqs = Db::table('novel_freq')->where('id','in',$freq_id_gh)->column('id,name');


            foreach ($list as $k => $v) {

                if($v['profit'] < 0)
                {
                    $list[$k]['profit']     = '-'.getQuotient(abs($v['profit']),100);
                }else{
                    $list[$k]['profit']     = getQuotient($v['profit'],100);
                }
                $list[$k]['accumulated_recharge_money'] = getQuotient($v['accumulated_recharge_money'],100);
                $list[$k]['cost']           = getQuotient($v['cost'],100);
                $list[$k]['bookname'] = isset($novels[$v['nid']]) ? $novels[$v['nid']]['title'] : '';
                $list[$k]['freq_name'] = isset($novels[$v['nid']]) && isset($freqs[$novels[$v['nid']]['freq_id']]) ? $freqs[$novels[$v['nid']]['freq_id']] : '';
                $list[$k]['cat_name'] = isset($novels[$v['nid']]) && isset($cats[$novels[$v['nid']]['class_id']]) ? $cats[$novels[$v['nid']]['class_id']] : '';
                $list[$k]['cat_name_son'] = isset($novels[$v['nid']]) && isset($cats_son[$novels[$v['nid']]['son_class_id']]) ? $cats_son[$novels[$v['nid']]['son_class_id']] : '';
                $list[$k]['t1']            = getQuotient($v['accumulated_recharge_money'],$v['cost'],2,'m',100).'%';
                $list[$k]['t2']            = $v['t2'] >= 0 ? $v['t2'].'%' : '';
                $list[$k]['t3']            = $v['t3'] >= 0 ? $v['t3'].'%' : '';
                $list[$k]['t4']            = $v['t4'] >= 0 ? $v['t4'].'%' : '';
                $list[$k]['t5']            = $v['t5'] >= 0 ? $v['t5'].'%' : '';
                $list[$k]['t6']            = $v['t6'] >= 0 ? $v['t6'].'%' : '';
            }

        }

        $result['count'] = $BookSummary->getTotalNumber($type, $where, $groupBy);
        $result['list']  = $list;

        $total_cost = isset($listAll) ? array_sum(array_column($listAll,'cost')) : 0;
        $total_recharge = isset($listAll) ? array_sum(array_column($listAll,'accumulated_recharge_money')) : 0;

        $result['summary']['total_cost'] = getQuotient($total_cost,100);
        $result['summary']['total_recharge'] = getQuotient($total_recharge,100);
        $result['summary']['total_recovery_rate'] = getQuotient($total_recharge,$total_cost,2,'m',100).'%';

        return $result;
    }

}